package com.qianfeng.xinyihezi.xinyihezi.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.activity.ShowActivity;
import com.qianfeng.xinyihezi.xinyihezi.model.GoodgoodBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by 张静远 on 2016/11/14.
 */

public class GoodRecycleAdapter extends RecyclerView.Adapter implements View.OnClickListener {
    private Context mContext;
    private List<GoodgoodBean> mData;
    private ILoadCallback mCallback;

    public GoodRecycleAdapter(Context mContext, List<GoodgoodBean> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case R.layout.item_good_recycler:
                View view = LayoutInflater.from(mContext).inflate(R.layout.item_good_recycler, null);
                holder = new ViewHolder(view);
                break;
            case R.layout.item_good_load:
                View view1 = LayoutInflater.from(mContext).inflate(R.layout.item_good_load, null);
                holder = new loadViewHolder(view1);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.mTextViewFirstTitle.setText(mData.get(position * 2).short_name);
            viewHolder.mTextViewFirstPrice.setText("￥" + mData.get(position * 2).p_price);
            viewHolder.mTextViewFirstCount.setText(mData.get(position * 2).like_count);
            viewHolder.mTextViewSecondTitle.setText(mData.get(position * 2 + 1).short_name);
            viewHolder.mTextViewSecondPrice.setText("￥" + mData.get(position * 2 + 1).p_price);
            viewHolder.mTextViewSecondCount.setText(mData.get(position * 2 + 1).like_count);
            Picasso.with(mContext).load(mData.get(position * 2).image_url).into(viewHolder.mImageViewFirst);
            Picasso.with(mContext).load(mData.get(position * 2 + 1).image_url).into(viewHolder.mImageViewSecond);
            viewHolder.mRelativeLayoutFirst.setOnClickListener(this);
            viewHolder.mRelativeLayoutFirst.setTag(position * 2);
            viewHolder.mRelativeLayoutSecond.setOnClickListener(this);
            viewHolder.mRelativeLayoutSecond.setTag(position * 2 + 1);
        } else if (holder instanceof loadViewHolder) {
            mCallback.onLoad();
        }
    }

    //对外暴露设置接口方法
    public void setLoadCallback(ILoadCallback callback) {
        this.mCallback = callback;
    }

    public void addData(List<GoodgoodBean> list) {
        mData.addAll(list);
        notifyDataSetChanged();//添加数据后通知 adpter 更新
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mData.size() / 2) {
            return R.layout.item_good_load;
        } else {
            return R.layout.item_good_recycler;
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : (mData.size() / 2) + 1;
    }

    @Override
    public void onClick(View v) {
        int tag = (int) v.getTag();
        Intent intent = new Intent(mContext, ShowActivity.class);
        switch (v.getId()) {
            case R.id.rl_good_first:
                intent.putExtra("id", mData.get(tag).goods_id);
                intent.putExtra("url", mData.get(tag).share_url);
                intent.putExtra("title", mData.get(tag).short_name);
                intent.putExtra("imageUrl", mData.get(tag).image_url);
                break;
            case R.id.rl_good_second:
                intent.putExtra("id", mData.get(tag).goods_id);
                intent.putExtra("url", mData.get(tag).share_url);
                intent.putExtra("title", mData.get(tag).short_name);
                intent.putExtra("imageUrl", mData.get(tag).image_url);
                break;
            default:
                break;
        }
        mContext.startActivity(intent);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout mRelativeLayoutFirst, mRelativeLayoutSecond;
        private ImageView mImageViewFirst, mImageViewSecond;
        private TextView mTextViewFirstTitle, mTextViewFirstPrice, mTextViewFirstCount, mTextViewSecondTitle, mTextViewSecondPrice, mTextViewSecondCount;

        public ViewHolder(View itemView) {
            super(itemView);
            mRelativeLayoutFirst = (RelativeLayout) itemView.findViewById(R.id.rl_good_first);
            mRelativeLayoutSecond = (RelativeLayout) itemView.findViewById(R.id.rl_good_second);
            mImageViewFirst = (ImageView) itemView.findViewById(R.id.iv_item_good_first);
            mImageViewSecond = (ImageView) itemView.findViewById(R.id.iv_item_good_second);
            mTextViewFirstTitle = (TextView) itemView.findViewById(R.id.tv_item_good_title_first);
            mTextViewFirstPrice = (TextView) itemView.findViewById(R.id.tv_item_good_price_first);
            mTextViewFirstCount = (TextView) itemView.findViewById(R.id.tv_item_good_count_first);
            mTextViewSecondTitle = (TextView) itemView.findViewById(R.id.tv_item_good_title_second);
            mTextViewSecondPrice = (TextView) itemView.findViewById(R.id.tv_item_good_price_second);
            mTextViewSecondCount = (TextView) itemView.findViewById(R.id.tv_item_good_count_second);
        }
    }

    public static class loadViewHolder extends RecyclerView.ViewHolder {

        public loadViewHolder(View itemView) {
            super(itemView);
        }
    }

    //回调接口，用于回调加载数据的方法
    public interface ILoadCallback {
        void onLoad();
    }
}
